package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import messages.SystemMessage;
import controlleur.Controlleur;
import controlleur.PromotionControlleur;
import controlleur.SuccursaleControlleur;

/**
 * Servlet implementation class PromotionServlet
 */
public class PromotionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;	
	Controlleur ctrl = new PromotionControlleur();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PromotionServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String action = request.getParameter(SystemMessage.ACTION);
		PrintWriter out = response.getWriter();
		if (action!= null)
		{

			if(action.equals(SystemMessage.GET_ALL_USER_PROMOTION))
			{				
				int lon = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LONGITUDE));
				int lat = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LATITUDE));			
				out.print(((PromotionControlleur) ctrl).getAllUserProm(lon,lat));
			}
			else if(action.equals(SystemMessage.GET_ALL_DETAILLANT_PROMOTION))
			{
				//TODO: a completer
				int lon = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LONGITUDE));
				int lat = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LATITUDE));
				//((PromotionControlleur)ctrl).getAllDetaillantProm(lon,lat);
			}
			else if(action.equals(SystemMessage.GET_USER_PROMOTION))
			{
				int idProm = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_ID));				
				out.print(((PromotionControlleur)ctrl).getUserProm(idProm));
			}
			else if(action.equals(SystemMessage.ADD_NEW_USER_PROMOTION))
			{
				int lon = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LONGITUDE));
				int lat = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LATITUDE));
				String nom = request.getParameter(SystemMessage.PROMOTION_NOM);
				String description =  request.getParameter(SystemMessage.PROMOTION_DESC);
				((PromotionControlleur)ctrl).createUserPromotion(nom, description, lon, lat);			
			}
			else if(action.equals(SystemMessage.DEL_PROMOTION))
			{
				int idProm = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_ID));
				((PromotionControlleur) ctrl).delProm(idProm);
			}
			else if(action.equals(SystemMessage.UPD_PROMOTION))
			{
				String nom = request.getParameter(SystemMessage.PROMOTION_NOM);
				String desc = request.getParameter(SystemMessage.PROMOTION_DESC);
				boolean usager = Boolean.parseBoolean(request.getParameter(SystemMessage.PROMOTION_USAGER));
				int type = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_TYPE));
				int lon = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LONGITUDE));
				int lat = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LATITUDE));
				int idProm = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_ID));
				
				((PromotionControlleur)ctrl).updatePromotion(nom, desc, usager, type, lon, lat, idProm);		
			}			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String action = request.getParameter(SystemMessage.ACTION);
		PrintWriter out = response.getWriter();	
		if(action != null)
		{
			if(action.equals(SystemMessage.ADD_NEW_USER_PROMOTION))
			{			
				int lon = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LONGITUDE));
				int lat = Integer.parseInt(request.getParameter(SystemMessage.PROMOTION_LATITUDE));
				String nom = request.getParameter(SystemMessage.PROMOTION_NOM);
				String description =  request.getParameter(SystemMessage.PROMOTION_DESC);
				((PromotionControlleur)ctrl).createUserPromotion(nom, description, lon, lat);			
			}
		}

	}

}
